package cate.game.activity.mooncake;

import cate.common.table.activity.mooncake.row.MooncakeWarOrderRow;
import cate.common.table.d.GDOperation;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.mooncake.msg.MooncakeWarOrderDataResp;
import cate.game.res.MixRes;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

public class MooncakeWarOrder extends SubActivitySingle<MooncakeActivity> {
    @NoteField(value = "普通奖励领取状态")
    public Map<Integer, Boolean> normalMap;

    @NoteField(value = "额外奖励领取状态")
    public Map<Integer, Boolean> extraMap;

    @NoteField("是否激活")
    public boolean active;

    @Override
    public void initialize(MooncakeActivity parent) {
        super.initialize(parent);
        if (normalMap == null) {
            normalMap = new HashMap<>();
        }
        if (extraMap == null) {
            extraMap = new HashMap<>();
        }
    }

    public void noticeUpdate() {
        role.sendNow(new MooncakeWarOrderDataResp(this));
    }

    public void onRoleEnd() {
        active = false;
        normalMap.clear();
        extraMap.clear();
    }

    public GameResult<Void> allowPay(PayItemRow payItemRow) {
        GameResult<Void> r = new GameResult<>();
        if (active) {
            return r.fail("不可重复激活");
        }
        return r;
    }

    public void onPaySuccess(PayItemRow payItemRow) {
        active = true;
//        role.getHistory().action.speCallWarOrderActive();
        noticeUpdate();
    }

    public GameResult<MooncakeWarOrder> takeReward(int tid) {
        GameResult<MooncakeWarOrder> r = new GameResult<>();
        r.data = this;
        MooncakeWarOrderRow row = findWarOrderRow(tid);
        if (row == null) {
            return r.fail("配置错误");
        }
        if (parent.challenge.totalPass()<row.challengeTimes) {
            return r.fail("挑战目标未达成");
        }
        MixRes reward = new MixRes();
        if (!normalMap.containsKey(tid)) {
            reward.addList(new MixRes(row.baseReward));
            normalMap.put(tid, true);
        }
        if (!extraMap.containsKey(tid) && active) {
            reward.addList(new MixRes(row.upgradeReward));
            extraMap.put(tid, true);
        }
        if (reward.isEmpty()) {
            return r.fail("没有可以领取的奖励");
        }
        reward.addAndDialog(role, true, GDOperation.MOONCAKE_WARORDER_REWARD,tid);
        return r;
    }

    public MooncakeWarOrderRow findWarOrderRow(int tid) {
        return role.getGame().table.mooncake.warOrder.get(tid);
    }
}
